package cn.tansanqinger.controller.login_register;
import java.util.Enumeration;
import java.util.Map;

/**
 * 处理关于教师注册账号的相关认证
 * @author C3H2
 * @time 11月
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tansanqinger.service.user.IUserMessageService;
import cn.tansanqinger.service.verification.IVerificationMessageService;
import cn.tansanqinger.util.PageData;

@Controller
@RequestMapping("/login/register")
public class TeacherRegisterController{
	
	@Autowired
	private IVerificationMessageService messageService;
	
	@Autowired
	private IUserMessageService userService;
	
	/**
	 * 获取相关首页
	 * @param request
	 * @return
	 */
	@RequestMapping("/teacher")
	public String registerRegisterAccount(HttpServletRequest request) {
		//System.err.println(request.getRequestURL().toString().replaceAll(request.getRequestURI().toLowerCase(),""));
		PageData pd = new PageData(request);
		return "stair/teacher_register";
	}
	
	/**
	 * 提交注册信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/submit")
	public String submitRegisterMessage(HttpServletRequest request) {
		PageData pd = new PageData(request);
		String emailVerification = messageService.readEmailMessageNoSQL(pd.get("email")+"");
		HttpSession session = request.getSession();
		if(emailVerification!=null&&pd.get("verificationCodeEmail")!=null) {
			if(emailVerification.equalsIgnoreCase(pd.get("verificationCodeEmail").toString())) {
				for (int i = 0; i < 4; i++) {
					String uuid = (String) session.getAttribute("count"+i);
					if(uuid==null&&"".equals(uuid)) {
						session.setAttribute("error", "注册失败，请认真填写数据！");
						return "redirect:/login/teacher_register";
					}
					pd.put("count"+i, uuid);
					session.removeAttribute("count"+i);
				}
				pd.put("project_url", request.getRequestURL().toString().replaceAll(request.getRequestURI().toLowerCase(),""));
				//存储数据
				
				if(userService.addTeacher(pd)) {
					session.setAttribute("message", "账号注册成功，等待账号激活！");
					return "redirect:/login";
				}else {
					session.setAttribute("error", "注册失败，请认真填写数据！");
					return "redirect:/login/teacher_register";
				}
			}
		}
		session.setAttribute("error", "注册失败，请认真填写数据！");
		return "redirect:/login/teacher_register";
	}
	
}
